﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Xml.Linq;

namespace YmatouAPI.Test
{
    /// <summary>
    /// 
    /// </summary>
    public class ApiUtils
    {
        public string AssemblyName { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public List<TypeEntity> ListAllRequest { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        //public List<TypeEntity> ListAllResponse { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public List<DocEntity> ListAllDoc { get; private set; }
        public ApiUtils(string _AssemblyName)
        {
            AssemblyName = _AssemblyName;
            Init();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        void Init()
        {
            ListAllDoc = new List<DocEntity>();
            ListAllRequest = new List<TypeEntity>();
            //ListAllResponse = new  List<TypeEntity>();
            //
            LoadAllDocs();
            LoadAllTypes();
        }
        void LoadAllTypes()
        {

            var strdllPath = string.Format(@"{0}\bin\{1}.dll",
                 HttpContext.Current.Server.MapPath("/"), AssemblyName);
            var fi = new System.IO.FileInfo(strdllPath);
            if (!fi.Exists)
            {
                return;
            }
            var assembly = Assembly.LoadFile(strdllPath);
            Type[] types = assembly.GetTypes();
            //
            foreach (Type t in types)
            {
                if (t.Name.EndsWith("Request", StringComparison.InvariantCultureIgnoreCase))
                {
                    ListAllRequest.Add(new TypeEntity { RequestNamespace = t.Namespace, RequestType = t, Summary = getSummary(t.FullName) });
                }
                //if (t.Name.EndsWith("Response", StringComparison.InvariantCultureIgnoreCase))
                //{
                //    ListAllResponse.Add(new TypeEntity { RequestNamespace = t.Namespace, RequestType = t, Summary = getSummary(t.FullName) });
                //}
            }
        }
        /// <summary>
        /// 
        /// </summary>
        void LoadAllDocs()
        {
            string strXmlPath = string.Format(@"{0}\bin\{1}.xml",
                HttpContext.Current.Server.MapPath("/"), AssemblyName);
            var fi = new System.IO.FileInfo(strXmlPath);
            if (!fi.Exists)
            {
                return;
            }
            var xdoc = XDocument.Load(strXmlPath);
            var members = xdoc.Root.Element("members").Elements("member");
            foreach (var m in members)
            {
                var firstAttributeValue = m.FirstAttribute.Value.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                ListAllDoc.Add(new DocEntity()
                {
                    TPF = firstAttributeValue[0],
                    FullName = firstAttributeValue[1],
                    Summary = m.Element("summary").Value.Trim()
                });
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestname"></param>
        /// <returns></returns>
        String getSummary(String fullName)
        {
            var doc = ListAllDoc.FirstOrDefault(x => x.FullName == fullName);
            if (doc != null)
            {
                return doc.Summary;
            }
            return string.Empty;
        }
    }
}